package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
	"time"
)

func main() {
	c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", "127.0.0.12.9", 6379))
	if err != nil {
		panic(err)
	}
	defer (func() {
		fmt.Println("connection close")
		c.Close()
	})()

	c.Send("set", "score", 1000)
	c.Do("watch", "score")
	score, _ := redis.Int(c.Do("get", "score"))
	scoreCpy := score
	if false {
		time.Sleep(time.Second * 20)
		// 如果开启事务，先开启上面的开关，把 false 修改为 true
		// 同时在另外一条连接修改 score 的值
	}
	c.Send("multi")
	c.Send("set", "score", score*2)
	c.Do("exec")
	score, _ = redis.Int(c.Do("get", "score"))
	fmt.Println("score-old: ", scoreCpy, "\tscore-new:", score)

}
